<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="发放时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra="审核后业绩奖励发放的时间"
        >
			<a-date-picker v-decorator="['time']" show-time placeholder="请选择时间" @change="onChange" @ok="onOk" />
        </a-form-item>
		<a-form-item
		  label="审核"
		  :labelCol="labelCol"
		  :wrapperCol="wrapperCol"
		  extra="审核通过请选择发放时间"
		>
		  <a-radio-group
		    v-decorator="['is_auth', {initialValue: 1}]"
		  >
		    <a-radio :value="1">通过</a-radio>
		    <a-radio :value="0">拒绝</a-radio>
		  </a-radio-group>
		</a-form-item>
		<a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
		  <a-input placeholder="请输入备注" v-decorator="['remark']" />
		</a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/user/kpi'
import { InputNumberGroup } from '@/components'

// 生成等级权重
const weights = []
for (let i = 1; i <= 20; i++) {
  weights.push(i)
}

export default {
  components: {
    InputNumberGroup
  },
  data () {
    return {
      // 对话框标题
      title: '审核',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
	  grant_time: '',
	  record: {}
    }
  },
  created () {

  },
  methods: {

    /**
     * 显示对话框
     */
    add (record) {
      // 显示窗口
      this.visible = true
	  // 当前记录
	  this.record = record
	  this.record.time = this.record.grant_time
	  // 设置默认值
	  this.setFieldsValue()
	  this.grant_time = this.record.grant_time
    },
	/**
	 * 设置默认值
	 */
	setFieldsValue () {
	  const { record, form: { setFieldsValue } } = this
	  this.$nextTick(() => {
	    setFieldsValue(pick(record, ['time', 'is_auth', 'remark']))
	  })
	},	
	onChange(value, dateString){
		this.grant_time = dateString
	},
	onOk(e){
		console.log(e)
	},
    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
		  values.grant_time = this.grant_time
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.auth({ rewardId: this.record.reward_id, form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
